package com.tyk.bookstore.front.product.util;

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.tyk.bookstore.front.product.model.entity.BookStatisticDo;
import com.tyk.bookstore.front.product.model.enume.BookSortOrder;

public class BookUtil {

    /**
     * 书籍统计数据取整
     */
    public static String round(Long v) {
        Long f;
        long j = 10L;
        while (true) {
            f = j;
            if (v < f) break;
            j = f * 10;
        }
        long r = (v / (f / 10)) * (f / 10);
        return v < 10000 ? String.valueOf(r) : (r / 10000) + "万";
    }

    /**
     * 书籍排序字段枚举转 BookStatisticDo属性Lambda表达式
     */
    public static SFunction<BookStatisticDo, ?> toField(BookSortOrder sortOrder) {
        return switch (sortOrder) {
            case MULTIPLE, SALE_NUM -> BookStatisticDo::getSaleNum;
            case PRICE -> BookStatisticDo::getPrice;
            case COMMENT_NUM -> BookStatisticDo::getCommentNum;
        };
    }
}
